﻿Namespace EIB.Bus.Addressing
    Public MustInherit Class EIBAddress
        Protected _address As UInt16 = 0


        Public Shared Function ParseAddress(address As UInt16, type As BusData.EIBBusData_FrameDestinationType) As EIBAddress
            Select Case type
                Case BusData.EIBBusData_FrameDestinationType.PhysicalAddress
                    Return New EIBPhysicalAddress(address)
                Case BusData.EIBBusData_FrameDestinationType.GroupAddress
                    Return New EIBGroupAddress(address)
                Case Else
                    Throw New EIB.Exceptions.EIBAddressParseException("Invalid address type.")
            End Select
        End Function

        Public Shared Function ParseAddress(address As Byte(), type As BusData.EIBBusData_FrameDestinationType) As EIBAddress
            Select Case type
                Case BusData.EIBBusData_FrameDestinationType.PhysicalAddress
                    Return New EIBPhysicalAddress(address)
                Case BusData.EIBBusData_FrameDestinationType.GroupAddress
                    Return New EIBGroupAddress(address)
                Case Else
                    Throw New EIB.Exceptions.EIBAddressParseException("Invalid address type.")
            End Select
        End Function


        Public Function ToByte() As Byte()
            Return New Byte() {(_address And &HFF00) >> 8, _address And &HFF}
        End Function

        Public Function ToUint16() As UInt16
            Return _address
        End Function


        Public Shared Operator =(a1 As EIBAddress, a2 As EIBAddress) As Boolean
            Return a1._address = a2._address
        End Operator

        Public Shared Operator <>(a1 As EIBAddress, a2 As EIBAddress) As Boolean
            Return a1._address <> a2._address
        End Operator


        Public MustOverride Function IsInvalid() As Boolean
    End Class
End Namespace